package com.lxk.service.impl;

import com.lxk.dao.AsMyMapper;
import com.lxk.dao.BillboardMapper;
import com.lxk.pojo.Billboard;
import com.lxk.pojo.BillboardExample;
import com.lxk.service.BillboardService;
import com.lxk.vo.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/7 19:17
 * @description
 */
@Service
@Transactional
public class BillboardServiceImpl implements BillboardService {

    @Autowired
    private AsMyMapper mm;
    @Autowired
    private BillboardMapper bm;

    public Integer getTotalPages(Integer num) {
        BillboardExample example = new BillboardExample();
        Integer count = (int)bm.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<Billboard> selBillboardPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<Billboard> list = mm.selBillboardPage(start, num, sort, order);
        PageModel<Billboard> model = new PageModel<Billboard>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public void addBillboard(Billboard billboard) {
        bm.insertSelective(billboard);
    }

    public Billboard selBillboard(Integer billboardId) {
        Billboard billboard = bm.selectByPrimaryKey(billboardId);
        return billboard;
    }

    public void updBillboard(Billboard billboard) {
        bm.updateByPrimaryKeySelective(billboard);
    }

    public void expireBillboard(Integer billboardId) {
        Billboard billboard = bm.selectByPrimaryKey(billboardId);
        Integer isExpired = billboard.getIsExpired();
        if (isExpired==0) {
            billboard.setIsExpired(1);
        }else {
            billboard.setIsExpired(0);
        }
        bm.updateByPrimaryKeySelective(billboard);
    }

    public void deleteBillboard(Integer billboardId) {
        bm.deleteByPrimaryKey(billboardId);
    }

    public void topBillboard(Integer billboardId) {
        Billboard billboard = bm.selectByPrimaryKey(billboardId);
        Integer isTop = billboard.getIsTop();
        if (isTop==0) {
            billboard.setIsTop(1);
        }else if(isTop==1) {
            billboard.setIsTop(0);
        }
        bm.updateByPrimaryKeySelective(billboard);
    }
}
